//
//  FLLLikeViewController.m
//  FLLGodOfCooking
//
//  Created by LL.F on 16/9/3.
//  Copyright © 2016年 LL.F. All rights reserved.
//

#import "FLLLikeViewController.h"
#import "ZWYNetTool.h"
#import "AFNetworking.h"
#import "FLLLikeModel.h"
#import "Masonry.h"
#import "FLLLikeTableViewCell.h"
#import "UIView+Frame.h"
#import "FLLMacro.h"
#import "LikeSecData.h"
#import "LikeData.h"
#import "LikeTags_Info.h"
#import "UIImageView+WebCache.h"
#import "FLLMakeFoodViewController.h"

@interface FLLLikeViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) FLLLikeModel *fllLikeModel;
@property (nonatomic, strong) UITableView *likeTableView;
@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) UIButton *bottomButton;
@property (nonatomic, strong) NSMutableArray<LikeSecData *> *tableArray;
@property (nonatomic, strong) NSArray<LikeTags_Info *> *collectionArray;
@property (nonatomic, copy) NSString *bodyString;
@property (nonatomic, assign) NSInteger *pageCount;
@property (nonatomic, assign) BOOL isLoading;
@property (nonatomic, assign) BOOL isLikeView;
@end

@implementation FLLLikeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.isLikeView = YES;
    [self createPOSTRequest];
    [self createLikeTableView];
    [self createBottomView];
}
- (void)createPOSTRequest
{
    
    if (self.idString.length == 0) {
        _isLikeView = YES;
        self.bodyString = @"page=1&methodName=UserLikes&size=20&version=4.40&";
    } else {
        _isLikeView = NO;
        self.bodyString = [NSString stringWithFormat:@"cat_id=%@&type=1&page=%zd&methodName=CategorySearch&size=20&version=4.40&", _idString, _pageCount];
        
        
    }
    [ZWYNetTool POST:@"http://api.izhangchu.com/ HTTP/1.1"
    andBody:_bodyString andBodyStyle:ZWYBodyString andHeader:@{@"Content-Type":@"application/x-www-form-urlencoded; charset=utf-8"} andResponse:ZWYJSON andSuccessBlock:^(id result) {
        
        // 刷新
        if (_isLoading == NO) {
            
            [self.tableArray removeAllObjects];
            _pageCount = 1;
            
        } else {
            _pageCount ++;
        }
        
          self.fllLikeModel = [FLLLikeModel modelWithDic:result];
        
        // 刷新
        if (_isLoading == NO) {
            
            self.tableArray = (NSMutableArray *)self.fllLikeModel.data.data;
            
        } else {
        
            [self.tableArray addObjectsFromArray:(NSMutableArray *)self.fllLikeModel.data.data];
            
        }
        
        if (_idString.length != 0 ) {
            NSString *finalString = [NSString stringWithFormat:@"%@ %@道", _titleString, self.self.fllLikeModel.data.total];
            self.navigationItem.title = finalString;
        }
       
        [self.likeTableView reloadData];
             } andFailureBlock:^(NSError *error) {
        
             }];
}
- (void)createLikeTableView
{

    self.likeTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    [self.view addSubview:_likeTableView];
    if (_idString.length != 0) {
        [self.likeTableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.mas_equalTo(0);
            make.bottom.mas_equalTo(0);
        }];
    } else {
    [self.likeTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(0);
        make.bottom.mas_equalTo(-49);
    }];
    }
    
    self.likeTableView.delegate = self;
    self.likeTableView.dataSource = self;
    self.likeTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.likeTableView registerNib:[UINib nibWithNibName:@"FLLLikeTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"likeTableCell"];
    
    if (_isLikeView == NO) {
         // 刷新
    self.likeTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _isLoading = NO;
        [self createPOSTRequest];
        [self.likeTableView.mj_header endRefreshing];
    }];
    // 加载
    self.likeTableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        _isLoading = YES;
        [self createPOSTRequest];
        [self.likeTableView.mj_footer endRefreshing];
    }];

    }
       
    
}
- (void)createBottomView
{

    self.bottomView = [[UIView alloc] init];
    [self.view addSubview:_bottomView];
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.height.mas_equalTo(49);
    }];
    
    UILabel *grayLabel = [[UILabel alloc] init];
    [_bottomView addSubview:grayLabel];
    [grayLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo (0);
        make.height.mas_equalTo(1);
    }];
    grayLabel.backgroundColor = FLLLightGrayColor;
    
    
    self.bottomButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.bottomView addSubview:_bottomButton];

        [self.bottomButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(grayLabel.mas_bottom);
        make.bottom.mas_equalTo(0);
        make.width.mas_equalTo(_bottomView.mas_width).multipliedBy(0.5);
        make.centerX.mas_equalTo(0);
    }];
    [self.bottomButton setTitle:@"登录下, 让掌厨更懂你" forState:UIControlStateNormal];
    [self.bottomButton setTitleColor:FLLOrangeColor forState:UIControlStateNormal];
    
}
#pragma mark -- TableView DataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  _tableArray.count;

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    FLLLikeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"likeTableCell"];
    cell.grayLabel.backgroundColor = FLLLightGrayColor;
    [cell.backgroundImage sd_setImageWithURL:[NSURL URLWithString:_tableArray[indexPath.row].image] placeholderImage:[UIImage new] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
    }];
    cell.titleLabel.text = _tableArray[indexPath.row].title;
    cell.titleLabel.font = [UIFont systemFontOfSize:15 * kWidthScale];
    cell.contentLabel.text = _tableArray[indexPath.row].description_data;
    cell.contentLabel.font = [UIFont systemFontOfSize:14 * kWidthScale];
    cell.didCount.text = [NSString stringWithFormat:@"%@人做过", _tableArray[indexPath.row].play];
    cell.didCount.font = [UIFont systemFontOfSize:14 * kWidthScale];
    
    self.collectionArray = _tableArray[indexPath.row].tags_info;
    cell.collectionArray = _collectionArray;
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return _likeTableView.fll_height / 2.0;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    NSString *idString = self.tableArray[indexPath.row].dishes_id;
    FLLMakeFoodViewController *makeFoodVC = [[FLLMakeFoodViewController alloc] init];
    makeFoodVC.idString = idString;
    [self.navigationController pushViewController:makeFoodVC animated:YES];
}
- (void)setIdString:(NSString *)idString
{

    _idString = idString;
    [self.likeTableView reloadData];
}
- (void)setTitleString:(NSString *)titleString
{

    _titleString = titleString;
    [self.likeTableView reloadData];
}
- (void)viewWillAppear:(BOOL)animated
{

    if (_idString.length != 0) {
        
        self.likeTableView.fll_height = SCREEN_HEIGHT;
        [_bottomView removeFromSuperview];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
